//
//  DKVideoRecordController.m
//  DKShortVideo
//
//  Created by Keer_LGQ on 2018/4/3.
//  Copyright © 2018年 DK. All rights reserved.
//

#import "DKRecordController.h"
#import "DKRecordVideoView.h"
#import "Masonry.h"
#import "DKRouterManger.h"

@interface DKRecordController ()<UIGestureRecognizerDelegate>
@property (nonatomic ,strong) DKRecordVideoView *recorder;
@property (nonatomic ,strong) UILabel *labRec;
@property (nonatomic ,strong) UIImageView *imageVideo;
@property (nonatomic ,copy) NSURL *newestVideoUrl;

@end

@implementation DKRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.recorder];
    
    UIView *baseView = [[UIView alloc] init];
    [baseView setBackgroundColor:[UIColor colorWithWhite:0.1 alpha:0.9]];
    [self.view addSubview:baseView];
    [baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.width.equalTo(self.view);
        make.height.mas_equalTo(self.view.frame.size.width/3);
    }];
    
    UILabel *labRecord = [[UILabel alloc] init];
    labRecord.userInteractionEnabled = YES;
    labRecord.textColor = [UIColor redColor];
    labRecord.text = @"开始";
    labRecord.textAlignment = NSTextAlignmentCenter;
    labRecord.backgroundColor = [UIColor blackColor];
    [baseView addSubview:labRecord];
    _labRec = labRecord;
    [labRecord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(baseView);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(40);
    }];

    [baseView addSubview:self.imageVideo];
    [_imageVideo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(baseView);
        make.left.equalTo(baseView).mas_offset(40);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(50);
    }];

    
    UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnBack addTarget:self action:@selector(onCloseView:) forControlEvents:UIControlEventTouchUpInside];
    [btnBack setBackgroundColor:[UIColor redColor]];
    [btnBack setTitle:@"返回" forState:UIControlStateNormal];
    [self.view addSubview:btnBack];
    [btnBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).mas_offset(25);
        make.left.equalTo(self.view).mas_offset(15);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(35);
    }];

}

- (void)dealloc
{
    NSLog(@"*******************************%@: dealloc", [self class]);
}

#pragma mark - play control
- (void)onCloseView:(UIButton *)btnBack
{
    [[DKRouterManger shareInstance] onRecordPreview:NO];
}

- (DKRecordVideoView *)recorder
{
    __weak typeof(self) weakSelf = self;

    if (!_recorder) {
        _recorder = [[DKRecordVideoView alloc] initWithFrame:self.view.bounds];
        [_recorder setRecordStopedBlock:^(UIImage *thumbImage, NSURL *newestUrl) {
            [weakSelf.imageVideo setImage:thumbImage];
            weakSelf.newestVideoUrl = newestUrl;
        }];
    }
    return _recorder;
}

- (UIImageView *)imageVideo
{
    if (!_imageVideo) {
        _imageVideo = [[UIImageView alloc] init];
        _imageVideo.userInteractionEnabled = YES;
        [_imageVideo setBackgroundColor:[UIColor colorWithWhite:0.4 alpha:0.9]];
    }
    return _imageVideo;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) {
        if (obj.view == _labRec) {
            [self.recorder toggleRecording];
            _labRec.text = @"停止";
        }else if (obj.view == _imageVideo) {
            [[DKRouterManger shareInstance] onRecordPreview:NO];
            [[DKRouterManger shareInstance] onPreviewVideo:self.newestVideoUrl.absoluteString isShow:YES];
        }
    }];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
    [touches enumerateObjectsUsingBlock:^(UITouch * _Nonnull obj, BOOL * _Nonnull stop) {
        if (obj.view == _labRec) {
            _labRec.text = @"开始";
            [self.recorder toggleRecording];
        }
    }];
}

@end
